import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store  from '../store'
import { toast } from "../utils/toast";
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'home'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/allgoods',
    name: 'AllGoods',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "Allgoods" */ '../views/AllGoods.vue')
  },
  {
    path: '/myOrders',
    name: 'MyOrders',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "MyOrders" */ '../views/MyOrders.vue')
  },
  {
    path: '/personal',
    name: 'Personal',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "Personal" */ '../views/Personal.vue'),
    redirect:'/personal/cart',
    children:[
      {
        // path:'/personal/cart' 完整写法
        // 简单写法
        path:'cart',
        name:'Cart',
        component: () => import(/* webpackChunkName: "Cart" */ '../components/personal/Cart.vue')
      },
      {
        path:'user',
        name:'User',
        component: () => import(/* webpackChunkName: "User" */ '../components/personal/User.vue')
      },
    ]
  },
  // {
  //   path: '/frees',
  //   name: 'Frees',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "Frees" */ '../views/Frees.vue')
  // },
  {
    path: '/goodsdetial',
    name: 'GoodsDetial',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "GoodsDetial" */ '../views/GoodsDetial.vue')
  },
  {
    path:'*',
    component: () => import(/* webpackChunkName: "NotFound" */ '../views/NotFound.vue')
  }
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 通过重写push方法 解决跳转到相同路由的报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
  const regPersonal = /\/personal\/cart/
  const regMyOrgers = /\/myOrders/
  // if(to.path==='/personal/cart'){
  if(regPersonal.test(to.path)||regMyOrgers.test(to.path)){
    const token = localStorage.getItem("x-auth-token");
    if (!token||!store.state.userInfo.username) {
      toast('请先登录','warning')
      store.commit('modifyShowLogin',true)
      return
    } 
  }
  next()
})
export default router
